<?php
declare (strict_types=1);

namespace app\admin\service\market\advert;

use app\admin\logic\market\advert\Advert as AdvertLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 广告位配置
 */
class Advert extends BaseService
{
    /**
     * @Inject()
     *
     * @var AdvertLogic
     */
    protected $advertLogic;

    /**
     * 配置列表
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-20
     *
     * @return   [type]                         [description]
     */
    public function index()
    {
        // 搜索功能
        $map = [
            'place'     => [],
            '__TABLE__' => [],
        ];
        Request::has('title', 'get', true) && $map['place'][] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        if (Request::has('status', 'get', true) && Request::get('status/d', -1) !== -1) {
            $map['__TABLE__'][] = ['status', '=', Request::get('status/d')];
        }
        $data = $this->advertLogic->getList($map);

        // 定义字段
        $keyList = [
            'title'       => ListItem::make()->title('标题(广告位置)'),
            'key'         => ListItem::make()->title('标识'),
            'app'         => ListItem::make()->title('所属应用'),
            'status'      => ListItem::make('custom')->title('状态'),
            'update_time' => ListItem::make()->title('最后更新时间'),
        ];
        // 设置表单
        $list = new PageList();
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = [
            'title'  => FormItem::make('input')->title(false)->placeholder('广告位置')->maxlength(30),
            'status' => FormItem::make('select')->title(false)->placeholder('状态')->options([
                ['title' => '不限制', 'value' => -1],
                ['title' => '启用', 'value' => 1],
                ['title' => '禁用', 'value' => 0],
            ]),
        ];
        $list->datas     = $data;

        return $list->send();
    }

    /**
     * 更新状态
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-05-25
     *
     * @param int    $id    [description]
     * @param string $field [description]
     * @return   [type]                                [description]
     */
    public function updateField(int $id, string $field)
    {
        $value = Request::put($field);
        $this->advertLogic->updateField($id, $field, $value);

        return $this->parseToData([], 1, '操作成功');

    }

    /**
     * 新建广告
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-22
     *
     * @return   [type]                         [description]
     */
    public function create()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $res  = $this->advertLogic->createAdvert($data);
            return $this->parseToData($res->toArray(), 1, '添加成功');
        }

        $places      = $this->advertLogic->getAdvertPlaceOptions();
        $placeholder = count($places) > 0 ? '请选择广告位置' : '暂无可添加的广告位';
        $keyList     = [
            'place'  => FormItem::make('select')->title('广告位置')->placeholder($placeholder)->options($places)->help('每个广告位置仅能添加一个广告')->disabled(count($places) <= 0)->required(),
            // 'status' => FormItem::make('slideSwitch')->title('状态')->activeValue(1)->inactiveValue(0),
            'remark' => FormItem::make('textarea')->placeholder('可输入100字的备注说明')->title('备注')->maxlength(100)->wordLimit()->rows(4),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey    = $keyList;
        $form->saveMethod = 'POST';

        $form->savePostUrl = '/admin/market/advert/create';

        return $form->send();
    }

    /**
     * 编辑广告
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-22
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function edit(int $id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $res  = $this->advertLogic->editAdvert($id, $data);
            return $this->parseToData($res->toArray(), 1, '编辑成功');
        }

        $data = $this->advertLogic->where('id', $id)->findOrFail();

        $places  = $this->advertLogic->getAdvertPlaceOptions($data->getAttr('place'));
        $keyList = [
            'place'  => FormItem::make('select')->title('广告位置')->placeholder('请选择广告位置')->options($places)->help('每个广告位置仅能添加一个广告')->disabled(count($places) <= 0)->required(),
            // 'status' => FormItem::make('slideSwitch')->title('状态')->activeValue(1)->inactiveValue(0),
            'remark' => FormItem::make('textarea')->placeholder('可输入100字的备注说明')->title('备注')->maxlength(100)->wordLimit()->rows(4),
        ];

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;
        // 表单字段
        $form->datas      = $data;
        $form->saveMethod = 'PUT';

        $form->savePostUrl = '/admin/market/advert/' . $id . '/edit';

        return $form->send();
    }

    /**
     * 删除广告
     * Author   Martinsun<syh@sunyonghong.com>
     * Date 2020-06-22
     *
     * @param int $id [description]
     * @return   [type]                             [description]
     */
    public function delete(int $id)
    {
        $res = $this->advertLogic->deleteAdvert($id);

        return $this->parseToData($res, 1, '删除成功');
    }
}
